package 剑指offer;

/**
 * @author sanji
 * @version 1.0
 * @since 1.8
 */

public class p24反转链表 {

    public ListNode reverseList1(ListNode head) {
        ListNode prev=null;//反转过程中用来指向null和前一个结点
        ListNode cur=head;//指向当前结点
        ListNode next=null;//暂存后继结点

        while (cur!=null) {
            next=cur.next;
            cur.next=prev;
            prev=cur;
            cur=next;
    }
        return prev;
   }


    public ListNode reverseList(ListNode head) {
        if (head==null) {
            return null;
        }
        if (head.next==null) {
            return head;
        }
        ListNode cur=head;
        ListNode prev = null;
        ListNode tmp = null;

        while (cur!=null) {
            tmp=cur.next;
            cur.next=prev;
            prev=cur;
            cur=tmp;
        }
        return prev;

    }


}
